package com.cloudlinkin.service.impl;

import java.util.TreeMap;
import java.util.UUID;

import com.cloudlinkin.constant.HfxContactConstant;
import com.cloudlinkin.entity.Contact;
import com.cloudlinkin.entity.HfxContactResult;
import com.cloudlinkin.mapper.ContactMapper;
import com.cloudlinkin.service.IContactsService;
import com.cloudlinkin.util.encrypt.Aes;
import com.cloudlinkin.util.http.HttpUtil;
import com.cloudlinkin.util.http.ParamUtilForHFX;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;

@Service
public class ContactsServiceImpl implements IContactsService {

	@Autowired
	private ContactMapper contactMapper;

	/**
	 * 获取联系人ID
	 * @param enterpriseId 企业id
	 * @param regMobile	手机号
	 * @return
	 */
	public HfxContactResult getContactId(String enterpriseId, String regMobile) {
		HfxContactResult result = new HfxContactResult();
		Contact c = new Contact();
		c.setRegMobile(regMobile);
		c.setEnterpriseId(enterpriseId);
		Contact contact = contactMapper.selectOne(c);
		if(null == contact || StringUtils.isEmpty(contact.getContactId())) {
			TreeMap<String, Object> paramMap;
			//如果DB没有联系人信息，则直接进行联系人添加
			String url = HfxContactConstant.envUrl + "/enterprise/addItem.json";
			try {
				paramMap = this.createItemMessage(enterpriseId, regMobile);
				String ret = HttpUtil.httpsPostForHfx2(url, paramMap);
				result = this.checkItemResultMessage(ret, enterpriseId, regMobile);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			result.setHfxStatus("0");
			result.setHfxMsg("成功");
			result.setContactId(contact.getContactId());
		}
		return result;
	}

	/**
	 * 创建报文
	 * @param enterpriseId 企业ID
	 * @param regMobile	手机号
	 * @return 参数map
	 * @throws Exception
	 */
	public TreeMap<String, Object> createItemMessage(String enterpriseId, String regMobile) throws Exception {
		TreeMap<String,Object> paramMap = new TreeMap<>();
		paramMap.put("app_key", HfxContactConstant.appKey);
		paramMap.put("once", UUID.randomUUID().toString().replace("-", "").substring(0, 12));
		paramMap.put("version", HfxContactConstant.version);
		paramMap.put("channel", HfxContactConstant.appKey);
		paramMap.put("sdk_from", HfxContactConstant.sdkFrom);
		
		paramMap.put("enterpriseId", enterpriseId);
		paramMap.put("departmentIds", "");
		paramMap.put("name", Aes.Encrypt(regMobile, HfxContactConstant.encoding_aes_key));
		paramMap.put("regMobile", Aes.Encrypt(regMobile, HfxContactConstant.encoding_aes_key));
		paramMap.put("euserType", "2");
		
		paramMap.put("signature", ParamUtilForHFX.toHfxParam(paramMap, HfxContactConstant.appSecret)); // 签名
		System.out.println("start:" + paramMap.toString());
		return paramMap;
	}

	/**
	 * 返回参数解析
	 * @param ret 返回的字符串
	 * @param enterpriseId	企业ID
	 * @param regMobile	手机号
	 * @return 指定返回类
	 */
	public HfxContactResult checkItemResultMessage(String ret, String enterpriseId, String regMobile) {
		HfxContactResult result = new HfxContactResult();
		JSONObject jobj = JSONObject.parseObject(ret);
		int code = (int) jobj.get("error_code");
		if(0 == code) {
			//执行入库操作并返回成功
			Contact c = new Contact();
			c.setContactId(jobj.getString("contactId"));
			c.setEnterpriseId(enterpriseId);
			c.setName(regMobile);
			c.setRegMobile(regMobile);
			int i = contactMapper.insert(c);
			if(i == 1) {
				result.setHfxStatus(code + "");
				result.setHfxMsg("联系人添加成功");
				result.setContactId(jobj.getString("contactId"));
			} else {
				result.setHfxStatus("1");
				result.setHfxMsg("联系人存储失败");
			}
		} else {
			result.setHfxStatus(code + "");
			result.setHfxMsg(jobj.getString("error_msg"));
		}
		return result;
	}
}
